package org.bigmarketapi.gatewaycenter.domain.manage.service;

import jakarta.annotation.Resource;
import org.bigmarketapi.gatewaycenter.domain.manage.model.vo.GwServerDetailVO;
import org.bigmarketapi.gatewaycenter.domain.manage.model.vo.GwServerVO;
import org.bigmarketapi.gatewaycenter.domain.manage.repository.ImanageRepository;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.common.Constants;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.dao.GwServerDao;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.dao.GwServerDetailDao;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.po.GwServer;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: Gateway-Center
 * @BelongsPackage: org.bigmarketapi.gatewaycenter.domain.manage.service
 * @Author: dujiaqi
 * @CreateTime: 2024-11-30  20:48
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class IManageServiceImpl implements IManageService {
    @Resource
    ImanageRepository manageRepository;


    @Override

    public List<GwServerVO> queryGatewayServerLIst() {
       return manageRepository.queryGatewayServerLIst();
    }

    @Override
    public boolean registerGatewayServerNode(String groupId, String gatewayId, String gatewayName, String gatewayAddress) {
        GwServerDetailVO gatewayServerDetailVO = manageRepository.queryGatewayServerDetail(gatewayId, gatewayAddress);
        if (null == gatewayServerDetailVO) {
            try {
                return manageRepository.registerGatewayServerNode(groupId, gatewayId, gatewayName, gatewayAddress, Constants.GatewayStatus.Available);
            } catch (DuplicateKeyException e) {
                return manageRepository.updateGatewayStatus(gatewayId, gatewayAddress, Constants.GatewayStatus.Available);
            }
        } else {
            return manageRepository.updateGatewayStatus(gatewayId, gatewayAddress, Constants.GatewayStatus.Available);
        }
    }

    @Override
    public GwServerDetailVO queryGatewayServerDetail(String gatewayId, String gatewayAddress) {
        return manageRepository.queryGatewayServerDetail(gatewayId, gatewayAddress);
    }

    @Override
    public boolean updateGatewayStatus(String gatewayId, String gatewayAddress, Integer available) {
       return manageRepository.updateGatewayStatus(gatewayId, gatewayAddress, available);
    }
}
